package com.loop.unclelend.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.loop.unclelend.databinding.UiRootviewBinding
import com.loop.unclelend.utils.BarUtil


/**
 * CustomDemo
 * @Author wwdeng
 * @CreateTime: 2024/6/11 10:46
 * @Annotation：基类 Fragment
 */
abstract class BaseFragment: Fragment(), BaseView {

    lateinit var rootViewBinding: UiRootviewBinding

    /* 是否拦截返回按钮，默认不拦截直接返回 */
    private var isInterceptBack = false

    private var mActivity: Activity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = activity
    }

    override fun getContext(): Context? {
        return mActivity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootViewBinding = UiRootviewBinding.inflate(inflater)
        rootViewBinding.layoutTitlebar.setPadding(0, BarUtil.getStatusBarHeight(),0,0)
        rootViewBinding.layoutContainer.addView(getUncleRootView(inflater), FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return rootViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setUncleTitleBarVisibility(View.GONE)
        //返回
        rootViewBinding.imgBack.setOnClickListener {
            onBackAction()
        }

        initUncleView()
        initUncleListener()
        initUncleData()
    }

    /**
     * 获取布局文件根视图
     *
     * @return
     */
    protected abstract fun getUncleRootView(inflater: LayoutInflater): View

    /**
     * 初始化 View 控件
     */
    protected abstract fun initUncleView()

    /**
     * 事件监听
     */
    protected abstract fun initUncleListener()

    /**
     * 加数据
     */
    protected abstract fun initUncleData()

    /**
     * @功能描述：是否显示Toolbar
     */
    fun setUncleTitleBarVisibility(visibility: Int) {
        rootViewBinding.layoutTitlebar.visibility = visibility
    }


    /**
     * 设置toolBar背景颜色
     */
    fun setUncleTitleBarColor(color: Int) {
        setUncleTitleBarVisibility(View.VISIBLE)
        rootViewBinding.layoutTitlebar.setBackgroundColor(color)
    }

    /**
     * 设置toolBar背景图片
     */
    fun setUncleTitleBarRes(res: Int) {
        setUncleTitleBarVisibility(View.VISIBLE)
        rootViewBinding.layoutTitlebar.setBackgroundResource(res)
        rootViewBinding.layoutTitlebar.setPadding(0, 0, 0, 0)
    }

    /**
     * 设置是否显示返回
     */
    fun setUncleBackBtnVisibility(visibility: Int) {
        rootViewBinding.imgBack.visibility = visibility
    }

    /**
     * 设置返回按钮图标
     */
    fun setUncleBackBtnRes(res: Int){
        setUncleBackBtnVisibility(View.VISIBLE)
        rootViewBinding.imgBack.setImageResource(res)
    }

    /**
     * 是否显示Title
     */
    fun setUncleTextTitleVisibility(visibility: Int){
        rootViewBinding.txtTitle.visibility = visibility
    }

    /**
     * 设置Title颜色
     */
    fun setUncleTextTitleColor(color: Int) {
        setUncleTextTitleVisibility(View.VISIBLE)
        rootViewBinding.txtTitle.setTextColor(color)
    }

    /**
     * 设置Title
     */
    fun setUncleTextTitle(text: String) {
        setUncleTextTitleVisibility(View.VISIBLE)
        rootViewBinding.txtTitle.text = text
    }

    /**
     * 设置Title对齐方式
     * 默认：Gravity.LEFT
     */
    fun setUncleTextTitleGravity(gravity: Int){
        setUncleTextTitleVisibility(View.VISIBLE)
        rootViewBinding.txtTitle.gravity = gravity
    }

    /**
     * 设置是否显示右侧图标
     */
    fun setUncleRightBtnVisibility(visibility: Int){
        rootViewBinding.imgKefu.visibility = visibility
    }

    /**
     * 设置右侧按钮图标
     */
    fun setUncleRightBtnRes(res: Int){
        setUncleRightBtnVisibility(View.VISIBLE)
        rootViewBinding.imgKefu.setImageResource(res)
    }

    override fun showUncleLoading() {
        val act = context as BaseActivity
        act.showUncleLoading()
    }

    fun showUncleLoading(msg: String="") {
        showUncleLoading(isTOuchOut = false, isCancel = true,msg)
    }

    fun showUncleLoading(isTOuchOut: Boolean, isCancel: Boolean,msg: String="") {
        val act = context as BaseActivity
        act.showUncleLoading(isTOuchOut,isCancel,msg)
    }

    override fun hideUncleLoading() {
        val act = context as BaseActivity
        act.hideUncleLoading()
    }

    override fun showUncleToast(s: CharSequence?) {
        showUncleToast(s.toString())
    }

    /**
     * toast
     * @param msg
     */
    fun showUncleToast(msg: String?) {
        val act = context as BaseActivity
        act.showUncleToast(msg)
    }

    /**
     * toast
     * @param msg
     */
    fun showUncleToastDuration(msg: String?, duration: Int) {
        val act = context as BaseActivity
        act.showUncleToastDuration(msg, duration)
    }

    /**
     * 页面跳转（不带参数）
     *
     * @param T
     */
    open fun onIntent(T: Class<*>?) {
        val act = context as BaseActivity
        act.onIntent(T)
    }

    /**
     * 页面跳转（带参数）
     *
     * @param T
     * @param bundle
     */
    open fun onIntent(T: Class<*>?, bundle: Bundle?) {
        val act = context as BaseActivity
        act.onIntent(T,bundle)
    }

    /* 隐藏键盘 */
    fun onHideSoftInput(v: View) {
        val act = context as BaseActivity
        act.onHideSoftInput(v)
    }

    /* 显示键盘 */
    fun onShowSoftInput(v: View) {
        val act = context as BaseActivity
        act.onShowSoftInput(v)
    }

    /* 返回 */
    fun onBackAction() {
        if (!isInterceptBack) { //不拦截，直接返回
            mActivity!!.finish()
        }
    }

    /* 是否拦截返回按钮 */
    fun isInterceptBack(isBackExit: Boolean) {
        isInterceptBack = isBackExit
    }

}
